//
//  ShopMoreListViewController.m
//  WZCLApp
//
//  Created by chirui on 2018/7/11.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "ShopMoreListViewController.h"
#import "MyCenterCViewCell.h"
#import "MyCenterCollectionReusableView.h"
#import "GoodsDetailsViewController.h"

@interface ShopMoreListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,SDCycleScrollViewDelegate>{
    
    UICollectionView * _collectionView;
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
    NSDictionary * dataDic;
    
    NSInteger  rowNumber;
    
    // 弹框加车 购买
    UIView * alertBgView;
    UILabel * priceLabel;  // 商品价格
    UILabel * kuCunNumber; // 库存
    UILabel * numberLabel;
    NSString * ChooseGoodsOption;  //选择类型
    
    UIScrollView * scrollView;
    UIView * tagView;
    int num; //购买数量
    int allNumber; // 总库存
    int currayIndex;
    
    UIView * noBodyView;
     NSString * option_id; //商品属性id
}


@end

@implementation ShopMoreListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    currayIndex = 0;
    num = 1;
    _dataArray = @[].mutableCopy;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor =[UIColor groupTableViewBackgroundColor];
    
    [self creatCollectionView];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_return_back" target:self action:@selector(backClick)];
    
    TitleView(self.titleStr);
    
    if ([self.classType isEqualToString:@"class"]) {
        [self getClassDataRequest];
    }else {
        
         [self getMainDataRequest];
    }
    
    noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-100, 200, 200) andMessageStr:@"还没有相关内容~" onView:nil];
   
}

-(void)backClick{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma creatCollectionView

-(void)creatCollectionView {
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-SH_TabBarHeight-SH_TopHeight) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:_collectionView];
    _collectionView.showsVerticalScrollIndicator = NO;
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        
        _page = 1;
        
        if ([self.classType isEqualToString:@"class"]) {
            [self getClassDataRequest];
        }else {
            
            [self getMainDataRequest];
        }
    }];
    _collectionView.mj_header = header;
    
        MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _page++;
    
            if ([self.classType isEqualToString:@"class"]) {
                [self getClassDataRequest];
            }else {
                [self getMainDataRequest];
            }
        }];
    
        footer.automaticallyRefresh = YES;
        _collectionView.mj_footer =footer;
    
        if (_dataArray.count>10) {
            _collectionView.mj_footer.hidden = NO;
        }else {
    
            _collectionView.mj_footer.hidden = YES;
        }

    //注册cell类
    [_collectionView registerNib:[UINib nibWithNibName:@"MyCenterCViewCell" bundle:nil] forCellWithReuseIdentifier:@"MyCenterCViewCell"];
    
    //    注册header类
    [_collectionView registerClass:[MyCenterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyCenterCollectionReusableView"];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _dataArray.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  
    NSDictionary * dic = _dataArray[indexPath.row];
    MyCenterCViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCenterCViewCell" forIndexPath:indexPath];
    cell.clipsToBounds = NO;
    
    [cell.goodsImage sd_setImageWithURL:ImageUrl(dic[@"cover_photo"]) placeholderImage:ImageName(@"img_new_default")];
    
    cell.gooseName.frame = CGRectMake(6, CGRectGetMaxY(cell.goodsImage.frame)+5, (WIDTH-10)/2-12, 20);
    cell.gooseName.text = dic[@"cate_name"];
    
    cell.gooseName.numberOfLines = 2;
    [cell.gooseName sizeToFit];
    //
    cell.scoreLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"sale_price"]];
    cell.scoreLabel.numberOfLines = 1;
    [cell.scoreLabel sizeToFit];
    
    cell.oldPrice.frame = CGRectMake(CGRectGetMaxX(cell.scoreLabel.frame)+5, CGRectGetMinY(cell.scoreLabel.frame)+2, 50, 15);
    cell.oldPrice.numberOfLines = 1;
    [cell.oldPrice sizeToFit];
    
    cell.addCarBtn.tag = indexPath.row;
    [cell.addCarBtn addTarget:self action:@selector(addCarBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [SHJudgeTools crossOutTheOriginalPrice:[NSString stringWithFormat:@"￥%@",dic[@"price"]] onLabel:cell.oldPrice];
    return cell;
}

-(void)addCarBtnClick:(UIButton *)btn{
    
    rowNumber = btn.tag;
    NSDictionary * dic = _dataArray[btn.tag];
    [self creatGoodsTypeUiFor:dic];
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //    NSDictionary * Dic = _dataArray[indexPath.row];
    GoodsDetailsViewController * goods =[[GoodsDetailsViewController alloc]init];
    //    goods.goodsId = [NSString stringWithFormat:@"%@",Dic[@"id"]];
    goods.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:goods animated:YES];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    //    第一个参数是宽，第二个参数是高 5s  1.8   6 1.6   6p
    float height;
    if (ISP5||ISP4) {
        height = 1.5;
    }else if (ISP6){
        height = 1.3;
        
    }else if (ISP6P){
        height = 1.4;
    }else {
        
        height = 1.4;
    }
    
    CGSize size=CGSizeMake((WIDTH-5)/2, (WIDTH-5)/2*height);
    
    return size;
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    //上 左 下 右
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

//设置水平间隙   默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

////设置垂直间隙  默认10
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  
        return CGSizeMake(0,10);

}
//设置footer的宽度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    //第一个参数：水平滑动时footer的宽度
    //第二个参数：垂直滑动时footer的高度
    return CGSizeMake(0, 0.01);
}

// 弹框

-(void)creatGoodsTypeUiFor:(NSDictionary *)dic{
    
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    alertBgView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    alertBgView.backgroundColor = ColorA(0, 0, 0, 0.5);
    
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300, WIDTH, HEIGHT - 300 - 48)];
    scrollView.bounces = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    //关闭水平滑块：默认YES
    scrollView.showsHorizontalScrollIndicator = NO;
    //关闭垂直滑块：默认YES
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.pagingEnabled = NO;
    
    NSDictionary * dict = dic[@"option"][currayIndex];
    
    priceLabel =[[UILabel alloc]initWithFrame:CGRectMake(110, 60, WIDTH -120, 20)];
    priceLabel.textColor = [UIColor orangeColor];
    priceLabel.text = [NSString stringWithFormat:@"￥%@",dict[@"price"]];
    priceLabel.font = [UIFont systemFontOfSize:15];
    
    [scrollView addSubview:priceLabel];
    
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 90, 90)];
    [image sd_setImageWithURL:ImageUrl(dic[@"cover_photo"]) placeholderImage:ImageName(@"")];;
    [scrollView addSubview:image];
    
    UILabel * nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(110, 30, WIDTH -140, 20)];
    nameLabel.text = dic[@"product_name"];
    nameLabel.font = [UIFont systemFontOfSize:16];
    [scrollView addSubview:nameLabel];
    
    UIView * backLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 15)];
    backLine.backgroundColor = ColorA(0, 0, 0, 0.5);
    [scrollView addSubview:backLine];
    
    UIView * backLine_two = [[UIView alloc]initWithFrame:CGRectMake(100, 0, WIDTH -100, 15)];
    backLine_two.backgroundColor = ColorA(0, 0, 0, 0.5);
    [scrollView addSubview:backLine_two];
    
    [self creatLineView:CGRectMake(0, 110, WIDTH, 0.5) andOnView:scrollView];
    
    UILabel * ggTitleLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, 130,90, 20)];
    ggTitleLabel.textColor = [UIColor blackColor];
    ggTitleLabel.text = @"规格选择";
    ggTitleLabel.font = [UIFont systemFontOfSize:15];
    [scrollView addSubview:ggTitleLabel];
    
    NSMutableArray * guiGeArray = @[].mutableCopy;
    if (guiGeArray.count>0) {
        [guiGeArray removeAllObjects];
    }
    
    for (NSDictionary * dicc in dic[@"option"]) {
        
        [guiGeArray addObject:dicc[@"option_name"]];
    }
    
    tagView = [self creatCellFitLabelOnArray:guiGeArray andBtnSel:@selector(chooseGuiGeBtn:) andViewFrame:CGRectMake(0, 160, WIDTH, 20) onView:scrollView];
    
    [self creatLineView:CGRectMake(0, CGRectGetMaxY(tagView.frame)+20, WIDTH, 0.5) andOnView:scrollView];
    
    UILabel * buyTitleLabel =[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(tagView.frame)+45,120, 20)];
    buyTitleLabel.textColor = [UIColor blackColor];
    buyTitleLabel.text = @"购买数量";
    buyTitleLabel.font = [UIFont systemFontOfSize:15];
    [scrollView addSubview:buyTitleLabel];
    
    
    kuCunNumber =[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(buyTitleLabel.frame), WIDTH- 110, 20)];
    kuCunNumber.textColor = [UIColor lightGrayColor];
    kuCunNumber.text = [NSString stringWithFormat:@"剩余%@件",dict[@"stock"]];;
    kuCunNumber.font = [UIFont systemFontOfSize:14];
    
    [scrollView addSubview:kuCunNumber];
    
    
    UIView * chooseNumber =[[UIView alloc]initWithFrame:CGRectMake(WIDTH-15 -140, CGRectGetMaxY(tagView.frame)+45, 140, 30)];
    chooseNumber.backgroundColor =[UIColor clearColor];
    chooseNumber.layer.borderWidth= 0.5f;
    chooseNumber.layer.borderColor =[UIColor lightGrayColor].CGColor;
    
    [scrollView addSubview:chooseNumber];
    
    [self creatShoppingAndCarBtnFrame:CGRectMake(0, 0, 40, 30) andImage:@"icon_popup_reduce_pre" andAction:@selector(subtractionBtn) andColor:[UIColor clearColor] andTitleColor:[UIColor blackColor] onView:chooseNumber];
    
    numberLabel = [[UILabel alloc]init];
    numberLabel.frame =CGRectMake(40, 0, 60, 30);
    
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.font =[UIFont systemFontOfSize:15];
    numberLabel.text = [NSString stringWithFormat:@"%d",num];
    [chooseNumber addSubview:numberLabel];
    
    [self creatShoppingAndCarBtnFrame:CGRectMake(100, 0, 40, 30) andImage:@"icon_popup_add" andAction:@selector(addBtn) andColor:[UIColor clearColor] andTitleColor:[UIColor blackColor] onView:chooseNumber];
    
    scrollView.contentSize = CGSizeMake(WIDTH , CGRectGetMaxY(chooseNumber.frame)+30);
    
    UIButton * deledateBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    deledateBtn.frame = CGRectMake(WIDTH - 50, 20, 40, 40);
    [deledateBtn setBackgroundImage:ImageName(@"icon_popup_close") forState:UIControlStateNormal];
    [deledateBtn addTarget:self action:@selector(deledateChooseView) forControlEvents:UIControlEventTouchUpInside];
    [scrollView addSubview:deledateBtn];
    [alertBgView addSubview:scrollView];
    
    UIView * btnView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-48, WIDTH, 48)];
    btnView.backgroundColor = [UIColor whiteColor];
    [alertBgView addSubview:btnView];
    
    UIButton * addCat = [UIButton buttonWithType:UIButtonTypeCustom];
    addCat.frame = CGRectMake(10, 0, WIDTH/2-20, 45);
    [addCat setBackgroundImage:ImageName(@"icon_botton__shop_blue") forState:UIControlStateNormal];
    [addCat setTitle:@"加入购物车" forState:UIControlStateNormal];
    [addCat addTarget:self action:@selector(addCatBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:addCat];
    
    
    UIButton * buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    buyBtn.frame = CGRectMake(WIDTH/2+10, 0, WIDTH/2-20, 45);
    [buyBtn setBackgroundImage:ImageName(@"icon_popup_botton_bg") forState:UIControlStateNormal];
    [buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
    [buyBtn addTarget:self action:@selector(buyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:buyBtn];
    
    
    [window addSubview:alertBgView];
}

-(void)buyBtnClick:(UIButton *)btn{ // 下单
    
    
    NSArray * ary = _dataArray[rowNumber][@"option"];
    
    NSDictionary * dict = ary[currayIndex];
    
    NSLog(@"dict=  %@  num  = %d",dict,num);
    
    option_id = dict[@"option_id"];
    
    [alertBgView removeFromSuperview];
    
    [self creatOrderBuyNowGoodsFor:dict[@"pro_id"]];
}

-(void)addCatBtnClick:(UIButton *)btn{ // 加车
    
    NSArray * ary = _dataArray[rowNumber][@"option"];
    
    NSDictionary * dict = ary[currayIndex];
    
    NSLog(@"dict=  %@  num  = %d",dict,num);
    
    option_id = dict[@"option_id"];
    
    [alertBgView removeFromSuperview];
    
    [self addShopCarGoodsFor:dict[@"pro_id"]];
}



-(void)deledateChooseView{
    
    [alertBgView removeFromSuperview];
    
}

-(void)chooseGuiGeBtn:(UIButton *)btn{
    
    currayIndex = (int)btn.tag -1000;
    
    
    if (tagView) {
        [tagView removeFromSuperview];
    }
    
    NSArray * ary = _dataArray[rowNumber][@"option"];
    
    NSDictionary * dict = ary[currayIndex];
    
    NSMutableArray * guiGeArray = @[].mutableCopy;
    if (guiGeArray.count>0) {
        [guiGeArray removeAllObjects];
    }
    
    for (NSDictionary * dicc in ary) {
        
        [guiGeArray addObject:dicc[@"option_name"]];
    }
    
    tagView = [self creatCellFitLabelOnArray:guiGeArray andBtnSel:@selector(chooseGuiGeBtn:) andViewFrame:CGRectMake(0, 160, WIDTH, 20) onView:scrollView];
    
    //    NSArray * priceArray = dataDic[@"options"];
    //    NSDictionary * detailsDic = dic[@"option"][currayIndex];
    //
    priceLabel.text = [NSString stringWithFormat:@"￥%@",dict[@"price"]];
    
    kuCunNumber.text =[NSString stringWithFormat:@"%@件",dict[@"stock"]];
    
}

//减按钮
-(void)subtractionBtn {
    num--;
    if (num <=1) {
        num= 1;
    }
    numberLabel.text =[NSString stringWithFormat:@"%d",num];
}
//加按钮
-(void)addBtn {
    
    NSArray * ary = _dataArray[rowNumber][@"option"];
    
    NSDictionary * dict = ary[currayIndex];
    
    num++;
    int  allNumber = [dict[@"stock"] intValue];
    
    if (num> allNumber) {
        //            num = allNumber;
        [SHJudgeTools MBProgressHUDPromptWith:@"没有库存了！" On:self.view];
        
    }else {
        numberLabel.text =[NSString stringWithFormat:@"%d",num];
        
    }
}

- (UIView *)creatCellFitLabelOnArray:(NSArray * )array andBtnSel:(SEL)sel andViewFrame:(CGRect)rec onView:(UIView *)view  {
    
    UIView * tagView =[[UIView alloc]initWithFrame:rec];
    
    [view addSubview: tagView];
    
    for (int i = 0; i < array.count; i++) {
        UIButton * _but = [UIButton buttonWithType:UIButtonTypeCustom];
        UIButton * upBut;
        _but.titleLabel.numberOfLines = 1;
        //        _but.layer.borderWidth = 0.8;
        //        _but.layer.borderColor = Color(245, 245, 245).CGColor;
        
        [_but setTitle:[NSString stringWithFormat:@"%@",array[i]] forState:UIControlStateNormal];
        [_but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_but setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_but setBackgroundImage:ImageName(@"icon_botton__shop_blue") forState:UIControlStateSelected];
        _but.backgroundColor = RGBa(0xf6f6f6) ;
        
        _but.titleLabel.font = [UIFont systemFontOfSize:15];
        _but.tag = 1000 + i;
        
        //        CategoryModel * model = self.dataSource[i];
        if (i == currayIndex) {
            
            _but.selected = YES;
        }else{
            
            _but.selected = NO;
        }
        
        [_but addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
        _but.titleLabel.textAlignment = NSTextAlignmentCenter;
        CGSize size=[_but.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
        if (i!=0) {
            upBut=(id)[tagView viewWithTag:999+i];
            _but.frame=CGRectMake(CGRectGetMaxX(upBut.frame)+10, CGRectGetMinY(upBut.frame), size.width+Wt_Text,Ht_Text);
            if (CGRectGetMaxX(_but.frame) > (VIEW_W(tagView))) {
                _but.frame=CGRectMake(10, CGRectGetMaxY(upBut.frame) + H_line, size.width+Wt_Text, Ht_Text);
            }
        }else{
            _but.frame=CGRectMake(10, 10, size.width+Wt_Text, Ht_Text);
        }
        _but.layer.cornerRadius = 5.f;
        _but.clipsToBounds = YES;
        
        //        [buttonArray addObject:_but];
        
        [tagView addSubview:_but];
        if (i == array.count - 1) {
            tagView.frame = CGRectMake(VIEW_XMIN(tagView), VIEW_YMIN(tagView), VIEW_W(tagView), VIEW_YMAX(_but)+10);
        }
        _but.enabled = YES;
        _but.userInteractionEnabled = YES;
    }
    
    
    
    return tagView;
}

-(void)creatShoppingAndCarBtnFrame:(CGRect)cgrect andImage:(NSString *)imageName andAction:(SEL)action andColor:(UIColor*)col andTitleColor:(UIColor *)titleCol onView:(UIView *)view {
    
    UIButton * carBtn =[UIButton buttonWithType:UIButtonTypeSystem];
    carBtn.frame =cgrect;
    [carBtn setBackgroundImage:ImageName(imageName) forState:UIControlStateNormal];
    [carBtn setTitleColor:titleCol forState:UIControlStateNormal];
    carBtn.titleLabel.font =[UIFont systemFontOfSize:15];
    [carBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:carBtn];
}

-(void)creatLineView:(CGRect)rec andOnView:(UIView *)view{
    
    UIView * line =[[UIView alloc]initWithFrame:rec];
    line.backgroundColor = RGBa(0xe5e5e5);
    [view addSubview:line];
}

// 获取数据 更多

-(void)getMainDataRequest{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_collectionView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",_page],
                           @"key":self.typeKey,
                           @"per_page":@"10"
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_moreGoods_api) andparameters:dic Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue]==200) {
            
            _totailPage = [responseObject[@"datas"][@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
                
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            if (_dataArray.count>0) {
                noBodyView.hidden =YES;
                if (_dataArray.count<10) {
                    
                    _collectionView.mj_footer.hidden = YES;
                }
            }else {
                _collectionView.mj_footer.hidden = YES;
                noBodyView.hidden =NO;
                
                [self.view addSubview:noBodyView];
            }
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
            
        }
        
        [_collectionView.mj_footer endRefreshing];
        [_collectionView.mj_header  endRefreshing];
        [_collectionView reloadData];

        
    } failed:^(NSError *error) {
        [_collectionView.mj_header endRefreshing];
        [_collectionView reloadData];
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
    
    
}

// 获取分类数据

-(void)getClassDataRequest{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_collectionView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{@"page":[NSString stringWithFormat:@"%d",_page],
                           @"cate_id":self.cateIdStr,
                           @"per_page":@"10"
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_classGoods_api) andparameters:dic Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue]==200) {
            
            _totailPage = [responseObject[@"datas"][@"total_page"]  intValue];
            
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
                
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            if (_dataArray.count>0) {
                noBodyView.hidden =YES;
                if (_dataArray.count<10) {
                    
                    _collectionView.mj_footer.hidden = YES;
                }
            }else {
                _collectionView.mj_footer.hidden = YES;
                noBodyView.hidden =NO;
                
                [self.view addSubview:noBodyView];
            }
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
            
        }
        
        [_collectionView.mj_footer endRefreshing];
        [_collectionView.mj_header  endRefreshing];
        [_collectionView reloadData];
        
        
    } failed:^(NSError *error) {
        [_collectionView.mj_header endRefreshing];
        [_collectionView reloadData];
        SHuiMBhudTextTimeStr(@"网络错误，请稍后再试");
    }];
    
    
}


/**
 *  加车
 */
-(void)addShopCarGoodsFor:(NSString *)proid{
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"pro_id":proid,
                           @"quantity":[NSString stringWithFormat:@"%d",num],
                           @"option_id":option_id
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(add_shopCart_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        
        if (codeSucess) {
            
            SHuiMBhudTextTimeStr(@"添加购物车成功！");
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
    
}

// 结算购买

-(void)creatOrderBuyNowGoodsFor:(NSString *)proid{
    
    NSDictionary * dic = @{@"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"pro_id":proid,
                           @"quantity":[NSString stringWithFormat:@"%d",num],
                           @"option_id":option_id
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(buy_itnowOrder_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        
        if (codeSucess) {
            
            SureOrderViewController * order = [[SureOrderViewController alloc]init];
            order.dataDic = responseObject[@"datas"];
            order.typeStr = @"goods";
            [self.navigationController pushViewController:order animated:YES];
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextNetError;
    }];
    
}

@end
